/**
 * [OElove Webapp] (C)2010-2099 oelove.com Inc.
 * Email: service@phpcoo.com，phpcoo@qq.com
 * This is not a freeware, use is subject to license terms
 * $ LastTime 2017.04.07 Update by CL $
*/
$(function(){
	
	/*------------------ 华丽分割线 add marry Begin ---------------------*/
	//打开发布页
	$(document).on("click", "[f='but_open_addmarry']", function(){
		$_url = _ROOT_PATH + "index.php?m=user&c=marry&a=add&datatype=json";
		oeShadeBox("mymarry_add", "ajax", $_url, 700, 520);
	});

	//样式单选
	$(document).on("click", "[f='select_radio_s']", function(){
		$_tab = $(this).attr("data-tab");
		$_val = $(this).attr("data-val");
		$("[data-tab='"+$_tab+"']").removeClass("current");
		$(this).addClass("current");
		$("#"+$_tab).val($_val);
	});
	
	//范围选择
	$(document).on("click", "[f='select_range']", function(){
		if ($(this).parent().parent().find("div").last().css("display") == "none") {
			$(this).parent().parent().parent().parent().addClass("current");
			$(this).parent().parent().find("div").last().show();
		}
		else {
			$(this).parent().parent().parent().parent().removeClass("current");
			$(this).parent().parent().find("div").last().hide();
		}
	});
	//确定选择
	$(document).on("click", "[f='confirm_range']", function(){
		$_tab1 = $(this).attr("data-1");
		$_tab2 = $(this).attr("data-2");
		$_unit = $(this).attr("data-unit");
		if (typeof($_unit) == "undefined") {
			$_unit = "";
		}
		$_text = "";

		$_val1 = $("#"+$_tab1).val();
		if (typeof($_val1) == "undefined") {
			$_val1 = "";
		}
		$_text1 = $("#"+$_tab1).find("option:selected").text();
		if ($_text1 == "请选择") {
			$_text1 = "";
		}

		$_val2 = $("#"+$_tab2).val();
		if (typeof($_val2) == "undefined") {
			$_val2 = "";
		}
		$_text2 = $("#"+$_tab2).find("option:selected").text();
		if ($_text2 == "请选择") {
			$_text2 = "";
		}
		if ($_text1.length > 0 && $_text2.length > 0) {
			$_text = $_text1+"-"+$_text2+$_unit;
		}
		if ($_text.length == 0) {
			$_text = "请选择";
		}

		$(this).parent().parent().hide();
		$(this).parent().parent().parent().find("div").first().find("span").html($_text+"<em></em>");
		$(this).parent().parent().parent().parent().parent().removeClass("current");

	});


	
	//保存发布
	$(document).on("click", "[f='but_save_addmarry']", function(){
		$catid = $("#catid").val();
		$title = $("#title").val();
		$endtimes = $("#endtimes").val();

		$thumbimg1 = $("#thumbimg1").val();
		if (typeof($thumbimg1) == "undefined") {
			$thumbimg1 = "";
		}
		$img1 = $("#img1").val();
		if (typeof($img1) == "undefined") {
			$img1 = ""; 
		}
		$thumbimg2 = $("#thumbimg2").val();
		if (typeof($thumbimg2) == "undefined") {
			$thumbimg2 = "";
		}
		$img2 = $("#img2").val();
		if (typeof($img2) == "undefined") {
			$img2 = "";
		}
		$thumbimg3 = $("#thumbimg3").val();
		if (typeof($thumbimg3) == "undefined") {
			$thumbimg3 = "";
		}
		$img3 = $("#img3").val();
		if (typeof($img3) == "undefined") {
			$img3 = "";
		}
		$thumbimg4 = $("#thumbimg4").val();
		if (typeof($thumbimg4) == "undefined") {
			$thumbimg4 = "";
		}
		$img4 = $("#img4").val();
		if (typeof($img4) == "undefined") {
			$img4 = "";
		}
		$thumbimg5 = $("#thumbimg5").val();
		if (typeof($thumbimg5) == "undefined") {
			$thumbimg5 = "";
		}
		$img5 = $("#img5").val();
		if (typeof($img5) == "undefined") {
			$img5 = "";
		}
		$thumbimg6 = $("#thumbimg6").val();
		if (typeof($thumbimg6) == "undefined") {
			$thumbimg6 = "";
		}
		$img6 = $("#img6").val();
		if (typeof($img6) == "undefined") {
			$img6 = "";
		}

		$ta_area1 = $("#ta_area1").val();
		$ta_area2 = $("#ta_area2").val();
		if (typeof($ta_area2) == "undefined") {
			$ta_area2 = "0";
		}
		$ta_gender = $("#ta_gender").val();
		$ta_age1 = $("#ta_age1").val();
		$ta_age2 = $("#ta_age2").val();
		$ta_height1 = $("#ta_height1").val();
		$ta_height2 = $("#ta_height2").val();
		$ta_weight1 = $("#ta_weight1").val();
		$ta_weight2 = $("#ta_weight2").val();
		$ta_marry = $("#ta_marry").val();
		$content = $("#content").val();

		if ($title.length == 0) {
			ToastShow("请填写标题");
			return false;
		}
		if ($endtimes.length == 0) {
			ToastShow("请选择有效时间");
			return false;
		}
		if ($ta_gender.length == 0) {
			ToastShow("请选择性别要求");
			return false;
		}
		if ($ta_age1.length == 0) {
			ToastShow("请选择年龄要求");
			return false;
		}
		if ($ta_age2.length == 0) {
			ToastShow("请选择年龄要求");
			return false;
		}

		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?m=user&c=marry",
			cache: false,
			data: {
				catid:$catid, title:$title, endtimes:$endtimes, thumbimg1:$thumbimg1, img1:$img1, thumbimg2:$thumbimg2, img2:$img2, 
				thumbimg3:$thumbimg3, img3:$img3, thumbimg4:$thumbimg4, img4:$img4, thumbimg5:$thumbimg5, img5:$img5, 
				thumbimg6:$thumbimg6, img6:$img6, ta_area1:$ta_area1, ta_area2:$ta_area2, ta_gender:$ta_gender,
				ta_age1:$ta_age1, ta_age2:$ta_age2, ta_height1:$ta_height1, ta_height2:$ta_height2, 
				ta_weight1:$ta_weight1, ta_weight2:$ta_weight2, ta_marry:$ta_marry, content:$content, 
				a:"saveadd", r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					if ($result.length > 0) {
						window.location.href = _ROOT_PATH+"index.php?c=pay&paynum="+$result; //在线支付
					}
					else {
						ToastShow("发布成功");
						_closeShadeBox("mymarry_add", function(){
							window.location.reload();
						});
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发布失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
	});

	//发布上传图片
	$(document).on("change", "#file_img_input", function(){
		$file_num = parseInt($("#file_num").val()); //当前第几张
		if (typeof($file_num) == "undefined") {
			$file_num = 1;
		}		
		//创建Li
		$append_li = "<li id='img_"+$file_num+"' class='loading'><span></span></li>";
		$("#addto_file_box").append($append_li);
		
		//上传照片
		$.ajaxFileUpload({
			"url": _ROOT_PATH+"index.php?m=user&c=index&a=upload&module=marry&thumbfiles=1",
			"secureuri": false,
			"fileElementId": $(this).attr("id"),
			"dataType": "json",
			"data": {uploadpart: $(this).attr("id")},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") { //上传成功
					$_html = "<img src='"+$imgthumb+"' />"+
								"<input type='hidden' id='thumbimg"+$file_num+"' value='"+$imgthumb+"' />"+
								"<input type='hidden' id='img"+$file_num+"' value='"+$imgurl+"' />";
					$("#img_"+$file_num).html($_html);
					$("#file_num").val($file_num+1);//下一张
					if ($file_num >= 6) {
						$("#upload_file_li").remove();
					}
					_oeloveFileSynYun($attid); //同步云存储
				}
				//失败
				else {
					$error = "上传失败，请检查图片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					ToastShow($error);
					$("#img_"+$file_num).remove();
				}

			},
			error: function ($data, e, b) {
				ToastShow("上传失败，请检查图片大小和格式！");
				$("#img_"+$file_num).remove();

			}
		});

	});


	/*------------------ 华丽分割线 add marry End ---------------------*/
	
	
	/*------------------ 华丽分割线 my marry Begin ---------------------*/

	//删除
	$(document).on("click", "[f='but_open_marrydel']", function(){
		$_id = $(this).attr("data-id");
		$_url = _ROOT_PATH+"index.php?m=user&c=marry&a=delete&id="+$_id;
		oeAjaxConfirmBox($_url, "确定要删除吗？", function(){
			$("#list_"+$_id).remove();
		});
	});

	//点击查看报名会员资料
	$(document).on("click", "[f='but_view_bmuser']", function(){
		$_id = $(this).attr("data-id");
		$_url = _ROOT_PATH + "index.php?m=user&c=marry&a=viewbmuser&id="+$_id+"&datatype=json";
		oeShadeBox("mymarry_viewbmuser", "ajax", $_url, 600, 400);
	});

	//选中报名人数
	$(document).on("click", "[f='but_select_bmuser']", function(){
		$_id = $(this).attr("data-id");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=user&c=marry",
			cache: false,
			data: {a:"selectbmuser", id:$_id, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;

				if ($response == "1") {
					ToastShow("操作成功");
					$("[f='but_select_bmuser']").remove();

					//重新加载
					$.ajax({
						type: "POST",
						url: _ROOT_PATH + "index.php?m=user&c=marry",
						cache: false,
						data: {a:"viewbmuser", id:$_id, r:get_rndnum(8)},
						dataType: "json",
						success: function($data) {
							$json = eval($data);
							$response = $json.response;
							$result = $json.result;
							if ($response == "1") {
								$("#mymarry_viewbmuser_data").html($result);
							}
						},
						error: function() {
						}
					});


				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("选取失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});

	/*------------------ 华丽分割线 my marry End ---------------------*/


	//弹出支付窗
	$(document).on("click", "[f='but_pay_marry']", function(){
		$_id = $(this).attr("data-id");
		$width = $(this).attr("data-width");
		if (typeof($width) == "undefined") {
			$width = 690;
		}
		$height = $(this).attr("data-height");
		if (typeof($height) == "undefined") {
			$height = 500;
		}
		$_url = _ROOT_PATH+"index.php?m=user&c=marry&a=pay&id="+$_id+"&r="+get_rndnum(8);
		oeShadeBox("pop_pay_marry", "ajax", $_url, $width, $height);
	});

	//选择支付方式
	$(document).on("click", "[f='sel_marry_payment']", function(){
		$("._list_marry_pay ul li").removeClass("current");
		$(this).addClass("current");
		$_sdkid = $(this).attr("data-sdkid");
		$_sdkname = $(this).attr("data-sdkname");
		
		$("#sdkid").val($_sdkid);
		$("#sdkname").val($_sdkname);
	});
	
	//提交支付
	$(document).on("click", "[f='but_marry_pay_submit']", function(){
		$_sdkid = $("#sdkid").val();
		$_sdkname = $("#sdkname").val();
		if ($_sdkid.length == 0) {
			ToastShow("请选择支付方式");
			return false;
		}
		$_id = $(this).attr("data-id");
		$.ajax({
			type: "POST",
			url: _OE_FILE+"&c=marry",
			cache: false,
			data: {
					a:"confirmpay", sdkid:$_sdkid, sdkname:$_sdkname, id:$_id, r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					window.location.href = _ROOT_PATH+"index.php?c=pay&a=submit&paynum="+$result;
				}
				else {
					$_error = $result;
					if ($_error.length == 0) {
						$_error = "提交失败，请检查";
					}
					ToastShow($_error);
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});

	
});
